<template>
  <el-dialog class="assign-role" title="分配角色" :visible="showRoleDialog" @close="closefn" @open="updataadd">
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="roleIds" v-loading="isLoading">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>

    <template #footer>
      <el-button type="primary" size="small" @click="updataAddfn">确定</el-button>
      <el-button size="small" @click="closefn">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getUserInfoAPI, updataadUserAPI } from '@/api/user'
import { getUSerInfoAPI } from '@/api/setting'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      roleIds: [],
      list: [],
      page: 1,
      pagesize: 22,
      isLoading: false
    }
  },
  methods: {
    closefn() {
      this.$emit('update:showRoleDialog', false)
    },
    async updataadd() {
      this.isLoading = true
      const res = await getUSerInfoAPI({
        page: this.page,
        pagesize: this.pagesize
      })
      console.log(res)
      this.list = res.data.rows

      /**
       * 回显接口
       */
      const ress = await getUserInfoAPI(this.userId)
      console.log(ress)
      this.roleIds = ress.data.roleIds || []

      this.isLoading = false
    },
    async updataAddfn() {
      const res = await updataadUserAPI({
        id: this.userId,
        roleIds: this.roleIds
      })
      console.log(res)
      this.$message.success('分配成功')
      this.closefn()
      this.updataadd()
    }
  }
}
</script>
